package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.ClickPhotoModles;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by lanouhn on 16/9/7.
 */
public class ClickPhotoAdapter extends RecyclerView.Adapter<ClickPhotoAdapter.PhotoViewHolder> {

    private Context context;
    private List<ClickPhotoModles> modlesList;

    public ClickPhotoAdapter(Context context, List<ClickPhotoModles> modlesList) {
        this.context = context;
        this.modlesList = modlesList;
    }

    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_click_photo, null);
        PhotoViewHolder holder = new PhotoViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(PhotoViewHolder holder, int position) {
        ImageOptions imageOptions = new ImageOptions.Builder()
                .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))//图片大小
                .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                .setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                .setUseMemCache(true)//缓存
                .setIgnoreGif(false)//不忽略动态图
                .build();
        x.image().bind(holder.imageView, modlesList.get(position).getImage_url(), imageOptions);
    }

    @Override
    public int getItemCount() {
        return modlesList.size();
    }

    public class PhotoViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;

        public PhotoViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.iv_item_click_photo);
        }
    }
}
